<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>Title</title>
  </head>
  <body>
  
  </body>
  
  <script>
    /* resolve：相当于new Promise参数回调函数内执行resolve()，参数操作完成时执行then。Promise.resolve的参数为Promise时将该参数Promise作为当前Promise的下一个then，再连接接下来的then或catch */
    Promise.resolve('res').then((res) => {console.log('then', res);});// then res
    Promise.resolve(new Promise((resolve) => { resolve('res'); })).then((res) => {console.log('then', res);});// then res
    Promise.resolve(new Promise(() => {})).then((res) => {console.log('then', res);});// 无输出
    
    /* reject：相当于new Promise参数回调函数内执行reject()，参数操作完成时执行catch。Promise.reject的参数整个作为catch的回调函数参数 */
    Promise.reject('res').catch((res) => {console.log('catch', res);});// catch res
    Promise.reject(new Promise((resolve) => {resolve('res');})).catch((res) => {console.log('catch', res);});// catch Promise {<fulfilled>: 'res'}
    Promise.reject(new Promise(() => {})).catch((res) => {console.log('catch', res);});// catch Promise {<pending>}
    
    /* any：参数为异步操作数组，任意异步操作完成时执行then，then回调函数参数为该异步成功响应，所有异步操作错误时执行catch，catch回调函数参数为异步失败响应 */
    Promise.any([
      new Promise((resolve) => {setTimeout(() => {resolve('res1');}, 4000);}),
      new Promise((resolve) => {setTimeout(() => {resolve('res2');}, 3000);}),
      new Promise((resolve) => {setTimeout(() => {resolve('res3');}, 2000);}),
    ]).then((res) => {
      console.log(res);// res3
    });
    Promise.any([
      new Promise((resolve, reject) => {setTimeout(() => {reject('res1');}, 4000);}),
      new Promise((resolve, reject) => {setTimeout(() => {reject('res2');}, 3000);}),
      new Promise((resolve, reject) => {setTimeout(() => {reject('res3');}, 2000);}),
    ]).catch((res) => {
      console.log(res);// AggregateError: All promises were rejected
    });
    
    /* all：参数为异步操作数组，所有异步操作完成时执行then，then回调函数参数与异步操作数组对应，任意异步操作错误时执行catch，catch回调函数参数为该异步错误响应 */
    Promise.all([
      new Promise((resolve) => {setTimeout(() => {resolve('res1');}, 4000);}),
      new Promise((resolve) => {setTimeout(() => {resolve('res2');}, 3000);}),
      new Promise((resolve) => {setTimeout(() => {resolve('res3');}, 2000);}),
    ]).then((res) => {
      console.log(res);// ['res1', 'res2', 'res3']
    });
    Promise.all([
      new Promise((resolve, reject) => {setTimeout(() => {reject('res1');}, 4000);}),
      new Promise((resolve, reject) => {setTimeout(() => {reject('res2');}, 3000);}),
      new Promise((resolve, reject) => {setTimeout(() => {reject('res3');}, 2000);}),
    ]).catch((res) => {
      console.log(res);// res3
    });
    
    /* race：参数为异步操作数组，任意异步操作完成或错误时执行then或catch，then和catch回到函数参数分别为异步成功或失败响应 */
    Promise.race([
      new Promise((resolve) => {setTimeout(() => {resolve('res1');}, 4000);}),
      new Promise((resolve) => {setTimeout(() => {resolve('res2');}, 3000);}),
      new Promise((resolve) => {setTimeout(() => {resolve('res3');}, 2000);}),
    ]).then((res) => {
      console.log(res);// res3
    });
    Promise.race([
      new Promise((resolve, reject) => {setTimeout(() => {reject('res1');}, 4000);}),
      new Promise((resolve, reject) => {setTimeout(() => {reject('res2');}, 3000);}),
      new Promise((resolve, reject) => {setTimeout(() => {reject('res3');}, 2000);}),
    ]).catch((res) => {
      console.log(res);// res3
    });
    
    /* allSettled：参数为异步操作数组，所有异步操作完成或错误时执行then，then回调函数参数与异步操作数组对应 */
    Promise.allSettled([
      new Promise((resolve, reject) => {setTimeout(() => {resolve('res1');}, 4000);}),
      new Promise((resolve, reject) => {setTimeout(() => {resolve('res2');}, 3000);}),
      new Promise((resolve, reject) => {setTimeout(() => {reject('res3');}, 2000);}),
    ]).then((res) => {
      console.log(res);// [{status: 'fulfilled', value: 'res1'}, {status: 'fulfilled', value: 'res2'}, {status: 'rejected', reason: 'res3'}]
    });
    Promise.allSettled([
      new Promise((resolve, reject) => {setTimeout(() => {resolve('res1');}, 4000);}),
      new Promise((resolve, reject) => {setTimeout(() => {reject('res2');}, 3000);}),
      new Promise((resolve, reject) => {setTimeout(() => {resolve('res3');}, 2000);}),
    ]).catch((res) => {
      console.log(res);// 无输出
    });
  </script>
</html>